这段代码为什么会出问题啊[delphi]

来源:百度知道 编辑:UC知道 时间:2024/05/20 01:38:27
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
FoundAtp: array of Integer;
nstart,nlength:integer;

length_of_deltext:integer;
StartPos, ToEnd: integer;
code_text:string;
begin
code_text:=codedit.Text;

Repeat
i:=0 ;
i:=i+1;
length_of_deltext:=0;
FoundAtp[i]:=pos('ssss',code_text) + length_of_deltext; { 运行可以,但是单击摁扭的时候就会在这里抱错.报错时出现一个对话框
---------------------------
Debugger Exception Notification
---------------------------
Project plh.exe raised exception class EAccessViolation with message 'Access violation at address 00453661 in module 'plh.exe'. Write of address 00000004'. Process stopped. Use Step or Run to continue.
---------------------------
OK Help
---------------------------
这是为什么啊 }

delete(code_text,pos('ssss',code_text),4);
length_of_deltext:=length(copy

procedure TForm1.Button1Click(Sender: TObject);
var
i, idx:integer;
FoundAtp: array of Integer;
code_text:string;
begin
code_text:=codedit.Text;
i := 0;
repeat
idx := Pos('ssss', code_text);
if idx > 0 then
begin
Inc(i);
SetLength(FoundAtp, i);
if i = 1 then
FoundAtp[i-1] := idx
else
FoundAtp[i-1] := FoundAtp[i-2] + idx + 4;

Delete(code_text, 1, idx+4);
end;
until idx <= 0;

for i:=Low(foundatp) to high(foundatp) do
begin
codedit.SelStart := foundatp[i]-1;
codedit.SelLength := 4;
codedit.SelAttributes.Color := clBlue;
end;
end;
1.你的动态数组的长度在不断地变,所以要不停地用SetLength来设定数组长度
2.动态数组的基数是从0开始,不是从1开始的

if语句中下面的语句如果多于1句,则可以不用begin..end,如果多于1句则必须用begin..end圈起来
inc(i)和i:=i+1的效果是一样的

加到50分吧,上面代码花了我老长时间!!~
现在看得比较少了,伯恩是谍影重重里的

FoundAtp: array of Integer
是动态数组,使